package day_2022_9_to_12.Day;

import java.util.Arrays;

/**
 * @author haomin
 * @date 2022/09/12 09:35
 **/
public class Day10_1_Num1608_SpecialArray {
    class Solution {
        public int specialArray(int[] nums) {
            for(int i = 0; i <= nums.length; ++i){
                int count = 0;
                for(int j = 0; j < nums.length; ++j){
                    if(nums[j] >= i) count++;
                }
                if(i == count) return count;
            }
            return -1;
        }
    }
    class Solution2 {
        public int specialArray(int[] nums) {
            int n = nums.length;
            Arrays.sort(nums);
            while(n > 0){
                int l = 0, r = nums.length, mid;
                while(l < r){
                    mid = l + ((r - l)>>1);
                    if(nums[mid] < n) l = mid + 1;
                    else r = mid;
                }
                if((nums.length - l) == n) return n;
                n--;
            }
            return -1;
        }
    }
}